<template>
  <div>
    <!-- 表单 -->
    <el-form style="width:500px"  ref="form" :rules="rules" :model="form" label-width="100px">
      <el-form-item label="姓名" prop="p_name">
        <el-input v-model="form.p_name" clearable/>
      </el-form-item>
      <el-form-item label="朝代" prop="dynasty">
        <el-input v-model="form.dynasty" clearable/>
      </el-form-item>
      <el-form-item label="生-死" prop="age">
        <el-input v-model="form.age" clearable/>
      </el-form-item>
      <el-form-item label="字" prop="deil">
        <el-input v-model="form.deil" clearable/>
      </el-form-item>
      <el-form-item label="首字母" prop="pinying">
        <el-input v-model="form.pinying" clearable/>
      </el-form-item>
      <el-form-item label="介绍" prop="count">
        <el-input v-model="form.count" type="textarea" autosize/>
      </el-form-item>
      <el-form-item label="艺术家图像" prop="icon">
        <el-upload class="avatar-uploader" 
          :action="`http://47.94.203.104:9000/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess" 
          :before-upload="beforeAvatarUpload">
          <img v-if="form.icon" :src="form.icon" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">立即新增</el-button>
        <el-button @click="$refs.form.resetFields()">重置</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import httpApi from "@/http/index.js"
export default {
  data() {
    return {
      form: {
        icon: '',
        p_name: '',
        dynasty: '',
        age: '',
        deil: '',
        count: '',
        pinying:''
      }, rules: {
        p_name: [{ required: true, message: "该字段必填", trigger: "blur" }],
        icon: [{ required: true, message: "该字段必填", trigger: "blur" }],
        deil: [{ required: true, message: "该字段必填", trigger: "blur" }],
        age: [{ required: true, message: "该字段必填", trigger: "blur" }],
        count: [{ required: true, message: "该字段必填", trigger: "blur" }],
        dynasty: [{ required: true, message: "该字段必填", trigger: "blur" }],
        pinying: [{ required: true, message: "该字段必填", trigger: "blur" }]
      }
    }
  },
  methods: {
    submit() {
      //表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          httpApi.artist.addArtist(this.form).then(res => {
            // console.log(res);
            this.$message({
              message: '新增成功',
              type: 'success'
            });
          })
          this.$refs.form.resetFields()
        } else {
          this.$message.error('新增失败，请稍后重试')
        }
      })
    },
    /** 上传成功后执行 */
    handleAvatarSuccess(res, file) {
      // console.log(res)
      // 返回图片地址
      this.form.icon= res.data
    },
    /** 上传文件的格式 / 大小验证 */
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
}
</script>

<style lang="scss" scoped> 
.avatar-uploader{
  ::v-deep .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius:6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}
.avatar {
  width: 128px;
  display: block;
}
</style>